import { NgModule, APP_INITIALIZER, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { API_BASE_URL } from '@shared/service-proxies/service-proxies';
import { AppConsts } from '@shared/AppConsts';
import { PlatformLocation } from '@angular/common';
import { AppPreBootstrap } from 'src/AppPreBootstrap';
import { AppSessionService } from '@shared/common/session/app-session.service';
import { CommonModule } from '@shared/common/common.module';
import { AbpModule } from 'abp-ng2-module/dist/src/abp.module';
import { HttpClientModule } from '@angular/common/http';
import { ServiceProxyModule } from '@shared/service-proxies/service-proxy.module';
export function getRemoteServiceBaseUrl(): string {
  return AppConsts.remoteServiceBaseUrl;
}
export function getBaseHref(platformLocation: PlatformLocation): string {
  let baseUrl = platformLocation.getBaseHrefFromDOM();
  if (baseUrl) {
      return baseUrl;
  }

  return '/';
}
function getDocumentOrigin() {
  if (!document.location.origin) {
      return document.location.protocol + '//' + document.location.hostname + (document.location.port ? ':' + document.location.port : '');
  }

  return document.location.origin;
}


export function appInitializerFactory(
  injector: Injector,
  platformLocation: PlatformLocation) {
  return () => {
      abp.ui.setBusy();

      return new Promise<boolean>((resolve, reject) => {
          AppConsts.appBaseHref = getBaseHref(platformLocation);
          let appBaseUrl = getDocumentOrigin() + AppConsts.appBaseHref;

          AppPreBootstrap.run(appBaseUrl, () => {
            let appSessionService: AppSessionService = injector.get(AppSessionService);
            appSessionService.init().then((result) => {
              abp.ui.clearBusy();
              resolve(result);
            }, (err) => {
                abp.ui.clearBusy();
                reject(err);
            });

          }, resolve, reject);
      });
  };
}
@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
     IonicModule.forRoot(),
     CommonModule.forRoot(),
     AppRoutingModule,
     AbpModule,
     ServiceProxyModule,
     HttpClientModule,
    ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    { provide: API_BASE_URL, useFactory: getRemoteServiceBaseUrl },
        {
            provide: APP_INITIALIZER,
            useFactory: appInitializerFactory,
            deps: [Injector, PlatformLocation],
            multi: true
        }
  ],

  bootstrap: [AppComponent]
})
export class AppModule {}
